package com.teamwork.ZuccEcho.Controller;

import com.alibaba.fastjson.JSON;
import com.teamwork.ZuccEcho.Entity.Modelquestion;
import com.teamwork.ZuccEcho.Entity.Moptions;
import com.teamwork.ZuccEcho.Entity.Qnoptions;
import com.teamwork.ZuccEcho.Entity.Qnquestion;
import com.teamwork.ZuccEcho.Repository.QnQuestionApiRepository;
import com.teamwork.ZuccEcho.Repository.QnoptionsApiRepository;
import com.teamwork.ZuccEcho.Result.ExceptionMsg;
import com.teamwork.ZuccEcho.Result.Response;
import com.teamwork.ZuccEcho.Result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/qnquesion")
public class QnQuestionApiController {
    protected Response result(ExceptionMsg msg){
        return new Response(msg);
    }
    protected Response result(){
        return new Response();
    }

    @Autowired
    private QnQuestionApiRepository qnQuestionApiRepository;
    @Autowired
    private QnoptionsApiRepository qnoptionsApiRepository;
    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    public List<Qnquestion> qnquestions;//所有问卷问题

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseData getQnQuestionList() {
        List<Qnquestion> list = new ArrayList<Qnquestion>(qnQuestionApiRepository.findAll());
        return new ResponseData(ExceptionMsg.SUCCESS,list);
    }

    //添加问卷题目
    @RequestMapping(value = "/add/v1/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addQnQuestion(@RequestBody Qnquestion qnquestion){
//        Modelquestion modelquestion = new Modelquestion();
//        modelquestion.setMq_name(mq_name);
//        modelquestion.setMq_content(mq_content);
//        modelquestion.setMq_type(mq_type);
//        modelquestion.setMq_isrequired(mq_isrequired);

        qnQuestionApiRepository.save(qnquestion);
        //获取刚刚添加的问卷问题的id
        long qnqid = qnQuestionApiRepository.findPublicedQnqId();
        List<Qnquestion> newlist = qnQuestionApiRepository.searchByQnQuestionId(qnqid);

        //如果不是填空题，就加入输入的选项
        if(qnquestion.getQnoptionsList().size()>0){
            List<Qnoptions> qnoptions=qnquestion.getQnoptionsList();
            for(int i=0;i<qnoptions.size();i++){
                Qnoptions qnoptions1=qnoptions.get(i);
                qnoptions1.setQnquestion(newlist.get(0));
                qnoptionsApiRepository.save(qnoptions1);
            }
        }

        return new ResponseData(ExceptionMsg.SUCCESS,qnquestion);
    }

    //删除问卷题目
    @RequestMapping(value = "/delete/v1/{id}", method = RequestMethod.POST)
    public Response deleteQnQuestion(@PathVariable("id") long id){
        //删除问卷题目的选项
        List<Long> optionsid=qnQuestionApiRepository.searchAllQnOptionsByQuestionId(id);
        for(int j=0;j<optionsid.size();j++){
            qnQuestionApiRepository.deleteOptionByOId(optionsid.get(j));
        }

        qnQuestionApiRepository.deleteById(id);
        return result(ExceptionMsg.SUCCESS);
    };

    //修改问卷题目
    @RequestMapping(value = "/put/v1/{id}", method = RequestMethod.POST)
    public ResponseData updateQnQuestion(@PathVariable("id") long id, @RequestBody Qnquestion qnquestion){
        qnQuestionApiRepository.modifyQnQuestionByQnqId(qnquestion.getQuestionnaire().getQn_id(),
                qnquestion.getQnq_type(),qnquestion.getQnq_name(),qnquestion.isQnq_isrequired(),
                qnquestion.getQnq_content(),id);
        return new ResponseData(ExceptionMsg.SUCCESS,qnquestion);
    };

    //查询问卷题目
    @RequestMapping(value = "/get/v1/{id}", method = RequestMethod.GET)
    public ResponseData findQnQuestion(@PathVariable("id") long id) throws IOException {
        List<Qnquestion> list = new ArrayList<>(qnQuestionApiRepository.searchByQnQuestionId(id));
        for (Qnquestion c : list) {
            c.getQnoptionsList();//懒加载
        }
        String strJSON = JSON.toJSONString(list);//将要返回的列表转为json字符串，返回给前端
        return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
    };

    //查
    @RequestMapping(value = "/re/{id}", method = RequestMethod.GET)
    public Qnquestion findQnQuestioned(@PathVariable("id") long id) throws IOException {
        RestTemplate client= restTemplateBuilder.build();
        String uri = "http://localhost:8080/qnquestion/"+id;
        System.out.println(uri);
        Qnquestion qnquestion = client.getForObject (uri,Qnquestion.class,id) ;

        return qnquestion;
    };
}
